package com.itheima.ydd.controller;

import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Article;
import com.itheima.ydd.entity.Cate;
import com.itheima.ydd.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 文章管理
 */
@Slf4j
@RestController
@RequestMapping("ydd_article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /***
     * Desc:文章列表-分页
     * @param start_time
     * @param end_time
     * @param title
     * @param cateid
     * @param page
     * @param limit
     * @return {@link ResultData}
     * @author Cr Mr
     * @date 2022/3/1 20:17
     */
    @GetMapping("list")
    public ResultData list(String start_time, String end_time, String title, String cateid, Integer page, Integer limit) {
        log.info("开始时间戳start_time:{},分类名称end_time:{},分类名称title:{},分类名称cateid:{},页码page:{},每页数量limit:{}",
                start_time, end_time, title, cateid, page, limit);
        HashMap<String, Object> map = articleService.getPageHashMap(start_time, end_time, title, cateid, page, limit);
        return ResultData.ok("", map);
    }

    /**
     * 文章删除
     *
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResultData del(Integer id) {
        log.info("文章删除id:{}", id);
        articleService.removeById(id);
        return ResultData.ok("", "文章删除成功");
    }

    /**
     * 文章添加
     *
     * @param article
     * @return
     */
    @PostMapping("add")
    public ResultData add(@RequestBody Article article) {
        log.info("文章添加Article:{}", article);
        article.setDefault_data("1");
        articleService.save(article);
        return ResultData.ok("", "文章添加成功");
    }


    /**
     * 返回文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(Integer id) {
        log.info("返回文章详情id:{}", id);
        Article article = articleService.getById(id);
        return ResultData.ok("", article);
    }

    /**
     * 文章修改
     *
     * @param article
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody Article article) {
        log.info(" 文章修改Cate:{}", article);
        articleService.updateById(article);
        return ResultData.ok("", " 文章修改成功");
    }


}
